Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attachment api #613

Merged
merged 9 commits into from
Dec 13, 2023
Merged

Attachment api #613

merged 9 commits into from
Dec 13, 2023

Conversation

p-avital
Copy link
Contributor

@p-avital p-avital commented Dec 5, 2023

No description provided.

@p-avital p-avital requested a review from Mallets December 5, 2023 15:29
}

#[zenoh_macros::unstable]
pub fn attachments_mut(&mut self) -> &mut Option<Attachments> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attachments returns Option<&Attachments>, attachments_mut should return Option<&mut Attachments>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the mutable accessor, I wanted to give the user the opportunity to straight up remove the attachments, so a reference to the whole option is needed

}

#[zenoh_macros::unstable]
pub fn attachments_mut(&mut self) -> &mut Option<Attachments> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should return Option<&mut Attachments>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

}

#[zenoh_macros::unstable]
pub fn attachments_mut(&mut self) -> Option<&mut Option<Attachments>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why double option?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above + getting the attachments on replies is fallible since error replies don't have attachments

); 10];
for (j, backer) in backer.iter_mut().enumerate() {
*backer = ((i * 10 + j).to_le_bytes(), (i * 10 + j).to_be_bytes())
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that the Iterator pattern being used here allows to avoid some extra copy. However, API-wise is very verbose and not-so elegant. Before going any further, I'd like to see a benchmark (e.g. pub/sub throughput) using the assert.insert(...) and the Iterator approach. I.e.: I'd like to understand the tradeoff between API usability and performance impact.

Copy link
Member

@sashacmc sashacmc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to add usage examples, preferably containing both approaches through an iterator and through a map.

self.slices.drain(drain_start..drain_end);
}
fn insert(&mut self, mut at: usize, slice: &[u8]) {
if slice.is_empty() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to check if at less than buffer size

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, but computing len here is a bit expensive... that check can be done by checking for slice_index == usize::MAX a bit later, adding to the todo list

pub fn iter(&self) -> AttachmentsIterator {
self.into_iter()
}
fn _get(&self, key: &[u8]) -> Option<ZSlice> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need _get, if it used only in the get? (the same for the insert)
Is it some rust syntax sugar?

Copy link
Contributor Author

@p-avital p-avital Dec 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for code size optimization: it's a rather common pattern when you want to provide a generic input that's immediately converted to a given type, as that conversion doesn't affect the rest of the function's behaviour.

Supposedly, an optimizer could do that for us, but this helps it to do that job by instead considering if it's worth inlining the common section or not, instead of looking for code sections that could be factorized

@sashacmc
Copy link
Member

sashacmc commented Dec 7, 2023

More thoughts.
Based on the name of the branch and for general reasons (we have en attachment which has map of key value pairs), I thought it would be correct to name the feature as an attachment not attachments and in my PR I already did it.
But there still some inconsistency between attachment and attachments.
I think we need to agree on a common name.
@Mallets

@p-avital
Copy link
Contributor Author

p-avital commented Dec 7, 2023

More thoughts. Based on the name of the branch and for general reasons (we have en attachment which has map of key value pairs), I thought it would be correct to name the feature as an attachment not attachments and in my PR I already did it. But there still some inconsistency between attachment not attachments. I think we need to agree on a common name. @Mallets

@Mallets agrees with you, I named the type Attachments because I view it as a collection of attachments, each attachment being named (so essentially, I call each value in the key-value pairs an attachment). I'm fine with the plural form being eliminated for consistency.

@sashacmc
Copy link
Member

sashacmc commented Dec 7, 2023

@Mallets agrees with you, I named the type Attachments because I view it as a collection of attachments, each attachment being named (so essentially, I call each value in the key-value pairs an attachment). I'm fine with the plural form being eliminated for consistency.

but what confuses me is that in rust we will have with_attachments/attachments methods but in the same time for the same functionality in C/C++ we will have as_attachment / set_attachment / attachment etc.

@p-avital
Copy link
Contributor Author

p-avital commented Dec 7, 2023

but what confuses me is that in rust we will have with_attachments/attachments methods but in the same time for the same functionality in C/C++ we will have as_attachment / set_attachment / attachment etc.

In Rust, it's common practice to use with_X(self, x) -> Self in builder patterns; set_X(&mut self, x: X)-> Option<X> being used for setters. Adding a setter could be done, but this role is mostly fulfilled by attachments_mut providing mutable access.

@@ -40,10 +53,28 @@ fn main() {
let mut count: usize = 0;
let mut start = std::time::Instant::now();
loop {
publisher.put(data.clone()).res().unwrap();
let attachments = (args.attachments_number != 0).then(|| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to remove the attachments from the z_pub_thr and to have it only on the z_pub/z_sub.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

z_sub example should also show how to use the attachment.

Copy link
Member

@sashacmc sashacmc Dec 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Mallets in zenoh-c I decided to move attachment usage to separate sample to don't overload main simple z_pub/z_sub samples.
Maybe we should do the same here too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine to move it to separate examples.

@@ -117,6 +121,22 @@ impl PutBuilder<'_, '_> {
self.kind = kind;
self
}

#[zenoh_macros::unstable]
pub fn attachment(&self) -> Option<&Attachment> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this more closely. What's the interest of attachment and attachment_mut in the PutBuilder? Shouldn't with_attachment be enough? It's a builder, so I would expect to be additive for the attachment...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed for builders


impl<'a> Publication<'a> {
#[zenoh_macros::unstable]
pub fn attachment(&self) -> Option<&Attachment> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment as for the PutBuilder. What's the interest of attachment and attachment_mut in the PutBuilder? Shouldn't with_attachment be enough? It's a builder, so I would expect to be additive for the attachment...

@@ -294,6 +306,22 @@ impl<'a, 'b, Handler> GetBuilder<'a, 'b, Handler> {
self
}

#[zenoh_macros::unstable]
pub fn attachment(&self) -> Option<&Attachment> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment as per other builders. What's the interest of attachment and attachment_mut in the PutBuilder? Shouldn't with_attachment be enough? It's a builder, so I would expect to be additive for the attachment...

@@ -150,6 +160,36 @@ pub struct ReplyBuilder<'a> {
result: Result<Sample, Value>,
}

impl<'a> ReplyBuilder<'a> {
#[zenoh_macros::unstable]
pub fn attachment(&self) -> Option<&Attachment> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment as in the other builders... What's the interest of attachment and attachment_mut in the PutBuilder? Shouldn't with_attachment be enough? It's a builder, so I would expect to be additive for the attachment...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, and this spot is especially painful

@Mallets Mallets merged commit 678cc57 into attachment Dec 13, 2023
12 of 15 checks passed
@Mallets Mallets deleted the attachment-api branch December 13, 2023 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants